package com.ftr.video.fpv.utils;

import android.media.MediaScannerConnection;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Surface;

import com.ftr.video.fpv.AppContext;
import com.serenegiant.usb.USBMonitor;

/**
 * Created by sknown on 2017/6/16.
 */

public class CameraHandler extends Handler {
    public static final int MSG_OPEN = 0;
    public static final int MSG_CLOSE = 1;
    public static final int MSG_PREVIEW_START = 2;
    public static final int MSG_PREVIEW_STOP = 3;
    public static final int MSG_CAPTURE_STILL = 4;
    public static final int MSG_CAPTURE_START = 5;
    public static final int MSG_CAPTURE_STOP = 6;
    public static final int MSG_MEDIA_UPDATE = 7;
    public static final int MSG_RELEASE = 9;

    private static String TAG = CameraHandler.class.getSimpleName();
    private boolean DEBUG = false;

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_OPEN:
                //thread.handleOpen((USBMonitor.UsbControlBlock)msg.obj);
                break;
            case MSG_CLOSE:
                //thread.handleClose();
                break;
            case MSG_PREVIEW_START:
                //thread.handleStartPreview((Surface)msg.obj);
                break;
            case MSG_PREVIEW_STOP:
                //thread.handleStopPreview();
                break;
            case MSG_CAPTURE_STILL:
                //thread.handleCaptureStill();
                break;
            case MSG_CAPTURE_START:
                //thread.handleStartRecording();
                break;
            case MSG_CAPTURE_STOP:
                //thread.handleStopRecording();
                break;
            case MSG_MEDIA_UPDATE:
                handleUpdateMedia((String)msg.obj);
                break;
            case MSG_RELEASE:
                //thread.handleRelease();
                break;
            default:
                throw new RuntimeException("unsupported message:what=" + msg.what);
        }
    }

    public void handleUpdateMedia(final String path) {
        if (DEBUG) Log.v(TAG, "handleUpdateMedia:path=" + path);

        try {
            if (DEBUG) Log.i(TAG, "MediaScannerConnection#scanFile");
            MediaScannerConnection.scanFile(AppContext.getInstance(), new String[]{ path }, null, null);
        } catch (final Exception e) {
            Log.e(TAG, "handleUpdateMedia:", e);
        }
    }
}
